package com.tkc.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.tkc.common.constant.PerformanceConstants;
import com.tkc.common.enums.BaseResultCodeEnum;
import com.tkc.common.exception.ResponseException;
import com.tkc.common.result.info.PageInfo;
import com.tkc.common.result.info.PerformanceInfo;
import com.tkc.common.util.RandomUtil;
import com.tkc.common.util.TimeHelperUtil;
import com.tkc.converter.DOOConvertToInfo;
import com.tkc.converter.DTOConvertToDOO;
import com.tkc.dao.TeacherDOOMapper;
import com.tkc.dao.model.doo.TeacherDOO;
import com.tkc.redis.UserDataService;
import com.tkc.redis.model.doo.UserData;
import com.tkc.service.TeacherServices;
import com.tkc.service.model.dto.TeacherDTO;
import com.tkc.service.model.info.TeacherInfo;

/**
 * 
 * @Function: service层实现
 * 
 * @Project :assessmenttask 
 * @Package :com.tkc.service.impl 
 * @File :TeacherServicesImpl.java 
 * @ClassName: TeacherServicesImpl  
 * 
 * @author tankaichao 
 * @email cgztkc@126.com 
 * @Date 2016年8月9日下午6:54:35
 * @since JDK 1.8 
 *
 */
@Service("teacherServices")
public class TeacherServicesImpl implements TeacherServices {
	
	@Autowired
	private TeacherDOOMapper teacherDOOMapper;
	
	@Autowired
	private UserDataService userDataService;
	
	/** 新增教师信息 **/
	@Override
	public int addTeacher(TeacherDTO teacherDTO) {
		TeacherDOO teacherDOO = DTOConvertToDOO.convertFromTeacherDTOToTeacherDOO(teacherDTO);
		teacherDOO.setId(RandomUtil.getLetterRandom());
		int value = teacherDOOMapper.insertTeacherSelective(teacherDOO);
		if (value < 1) {
			throw new ResponseException(BaseResultCodeEnum.ERR_CODE_10000);
		} else {
			return value;
		}
	}
	
	/** 查询教师信息 **/
	@Override
	public List<TeacherInfo> selectTeacherByPage(TeacherDTO teacherDTO, PageInfo pageInfo) {
		TeacherDOO teacherDOO = DTOConvertToDOO.convertFromTeacherDTOToTeacherDOO(teacherDTO);
		List<TeacherDOO> teacherDOOs = teacherDOOMapper.selectTeacherByPage(teacherDOO, pageInfo);
		if (0 == teacherDOOs.size()) {
			throw new ResponseException(BaseResultCodeEnum.ERR_CODE_40001);
		}
		int items = teacherDOOMapper.selectTeacherListSelective(teacherDOO).size();
		pageInfo.setItems(items);
		pageInfo.setTotalPage((items - 1) / pageInfo.getItemsPerPage() + 1);
		pageInfo.setCurItems(teacherDOOs.size());
		return DOOConvertToInfo.convertFromTeacherDOOsToTeacherInfos(teacherDOOs);
	}
	
	
	/** 修改教师信息 **/
	@Override
	public int updateTeacher(TeacherDTO teacherDTO) {
		int value = teacherDOOMapper.updateTeacherByPrimaryKeySelective(DTOConvertToDOO.convertFromTeacherDTOToTeacherDOO(teacherDTO));
		if (value < 1) {
			throw new ResponseException(BaseResultCodeEnum.ERR_CODE_10001);
		} else {
			return value;
		}
	}
	
	
	/** 删除教师信息 **/
	@Override
	public int deleteTeacher(TeacherDTO teacherDTO) {
		int value = teacherDOOMapper.deleteTeacherSelective(DTOConvertToDOO.convertFromTeacherDTOToTeacherDOO(teacherDTO));
		if (value < 1) {
			throw new ResponseException(BaseResultCodeEnum.ERR_CODE_40000);
		} else {
			return value;
		}
	}
	
	/** 逐条新增千万条教师信息 **/
	@Transactional
	public PerformanceInfo addCroreTeacherOneToOne(TeacherDTO teacherDTO) {
		PerformanceInfo performanceInfo = new PerformanceInfo();
		TeacherDOO teacherDOO = DTOConvertToDOO.convertFromTeacherDTOToTeacherDOO(teacherDTO);
		performanceInfo.setType(PerformanceConstants.Perform_Type.get(0));
		performanceInfo.setStrategy(PerformanceConstants.Perform_Strategy.get(0));
		performanceInfo.setStartTime(TimeHelperUtil.getSystemTime());
		long startTime = System.currentTimeMillis();
		//模拟数据向教师表逐条插入1000W条数据
		for (int i = 0; i < PerformanceConstants.Perform_InsertTotal.get(0); i++) {
			teacherDOO.setId(RandomUtil.getLetterRandom());
			int value = teacherDOOMapper.insertTeacherSelective(teacherDOO);
			if (value < 1) {
				throw new ResponseException(BaseResultCodeEnum.ERR_CODE_10000);
			}
		}
		performanceInfo.setEndTime(TimeHelperUtil.getSystemTime());
		long endTime = System.currentTimeMillis();
		long executiveTime = endTime - startTime;
		performanceInfo.setExecutiveTime(TimeHelperUtil.transformExecutiveTime(executiveTime) );
		return performanceInfo;
	}
	
	
	/** redis新增教师的登录信息  **/
	@Override
	public PerformanceInfo addCroreTeacherToRedis(TeacherDTO teacherDTO) {
		PerformanceInfo performanceInfo = new PerformanceInfo();
		TeacherDOO teacherDOO = DTOConvertToDOO.convertFromTeacherDTOToTeacherDOO(teacherDTO);
		performanceInfo.setType(PerformanceConstants.Perform_Type.get(0));
		performanceInfo.setStrategy(PerformanceConstants.Perform_Strategy.get(0));
		performanceInfo.setStartTime(TimeHelperUtil.getSystemTime());
		long startTime = System.currentTimeMillis();
		UserData userData = new UserData();
		userData.setName(teacherDOO.getName());
		userData.setPassword(teacherDOO.getPassword());
		userDataService.insertUserData(userData.getName(), userData);
		//向redis插入1000W条教师登录数据
		for (int i = 0; i < PerformanceConstants.Perform_InsertTotal.get(0)-1; i++) {
			UserData userData2 = new UserData();
			userData2.setName(RandomUtil.getNumberRandom(3));
			userData2.setPassword(RandomUtil.getNumberRandom(8));
			userDataService.insertUserData(userData2.getName(), userData2);
		}
		performanceInfo.setEndTime(TimeHelperUtil.getSystemTime());
		long endTime = System.currentTimeMillis();
		long executiveTime = endTime - startTime;
		performanceInfo.setExecutiveTime(TimeHelperUtil.transformExecutiveTime(executiveTime) );
		return performanceInfo;
	}
	
	/** 批量新增千万条教师信息 **/
	@Transactional
	public PerformanceInfo addCroreTeacherBulk(TeacherDTO teacherDTO) {
		PerformanceInfo performanceInfo = new PerformanceInfo();
		List<TeacherDOO> list = new ArrayList<TeacherDOO>();
		TeacherDOO teacherDOO = DTOConvertToDOO.convertFromTeacherDTOToTeacherDOO(teacherDTO);
		//开始模拟数据向教师表逐条插入1000W条数据
		for (int i = 0; i < PerformanceConstants.Perform_InsertMemberNum.get(0); i++) {
			teacherDOO.setId(RandomUtil.getLetterRandom());
			list.add(teacherDOO);
		}
		int value = 1;
		performanceInfo.setType(PerformanceConstants.Perform_Type.get(0));
		performanceInfo.setStrategy(PerformanceConstants.Perform_Strategy.get(1));
		performanceInfo.setStartTime(TimeHelperUtil.getSystemTime());
		long startTime = System.currentTimeMillis();
		for (int i = 0; i < PerformanceConstants.Perform_InsertNum.get(0); i++) {
			value = teacherDOOMapper.insertTeacherBulkSelective(list);
			if (value < 1) {
				throw new ResponseException(BaseResultCodeEnum.ERR_CODE_10000);
			}
		}
		performanceInfo.setEndTime(TimeHelperUtil.getSystemTime());
		long endTime = System.currentTimeMillis();
		long executiveTime = endTime - startTime;
		performanceInfo.setExecutiveTime(TimeHelperUtil.transformExecutiveTime(executiveTime) );
		return performanceInfo;
	}
	
	/** 存储过程新增千万条教师信息 **/
	@Transactional
	public PerformanceInfo addCroreTeacherStoredprocedure(TeacherDTO teacherDTO) {
		PerformanceInfo performanceInfo = new PerformanceInfo();
		HashMap<String, String> map = DTOConvertToDOO.convertFromTeacherDTOToTeacherDOOMap(teacherDTO);
		performanceInfo.setType(PerformanceConstants.Perform_Type.get(0));
		performanceInfo.setStrategy(PerformanceConstants.Perform_Strategy.get(2));
		performanceInfo.setStartTime(TimeHelperUtil.getSystemTime());
		long startTime = System.currentTimeMillis();
		int value = teacherDOOMapper.insertTeacherStoredprocedureSelective(map);
		if (value < 1) {
			throw new ResponseException(BaseResultCodeEnum.ERR_CODE_10000);
		}
		performanceInfo.setEndTime(TimeHelperUtil.getSystemTime());
		long endTime = System.currentTimeMillis();
		long executiveTime = endTime - startTime;
		performanceInfo.setExecutiveTime(TimeHelperUtil.transformExecutiveTime(executiveTime) );
		return performanceInfo;
	}
	
	/** 清空教师信息 **/
	@Override
	public int clearTeacher() {
		int value =teacherDOOMapper.clearTeacherDate();
		return value;
	}
	
	
	/** 登录校验**/
	@Transactional
	public Map<String, Object> selectTeacherByAuth(TeacherDTO teacherDTO) {
		PerformanceInfo performanceInfo = new PerformanceInfo();
		Map<String, Object> map = new HashMap<String,Object>();
		TeacherDOO teacherDOO = DTOConvertToDOO.convertFromTeacherDTOToTeacherDOO(teacherDTO);
		performanceInfo.setType(PerformanceConstants.Perform_Type.get(1));
		performanceInfo.setStrategy(PerformanceConstants.Perform_Strategy.get(3));
		performanceInfo.setStartTime(TimeHelperUtil.getSystemTime());
		long startTime = System.currentTimeMillis();
		TeacherDOO doo = teacherDOOMapper.selectTeacherSelective(teacherDOO);
		performanceInfo.setEndTime(TimeHelperUtil.getSystemTime());
		long endTime = System.currentTimeMillis();
		long executiveTime = endTime - startTime;
		performanceInfo.setExecutiveTime(TimeHelperUtil.transformExecutiveTime(executiveTime) );
		if (null == doo) {
			map.put("teacherInfo",BaseResultCodeEnum.ERR_CODE_30002.getMsg());
		}
		else{
			map.put("teacherInfo", doo);
		}
		
		map.put("performanceInfo", performanceInfo);
		return map;
	}
	
	/** Redis登录校验**/
	@Override
	public Map<String, Object> selectTeacherByRedisAuth(TeacherDTO teacherDTO) {
		PerformanceInfo performanceInfo = new PerformanceInfo();
		Map<String, Object> map = new HashMap<String,Object>();
		TeacherDOO teacherDOO = DTOConvertToDOO.convertFromTeacherDTOToTeacherDOO(teacherDTO);
		performanceInfo.setType(PerformanceConstants.Perform_Type.get(1));
		performanceInfo.setStrategy(PerformanceConstants.Perform_Strategy.get(3));
		performanceInfo.setStartTime(TimeHelperUtil.getSystemTime());
		long startTime = System.currentTimeMillis();
		UserData userData = userDataService.selectUserData(teacherDOO.getName());
		performanceInfo.setEndTime(TimeHelperUtil.getSystemTime());
		long endTime = System.currentTimeMillis();
		long executiveTime = endTime - startTime;
		performanceInfo.setExecutiveTime(TimeHelperUtil.transformExecutiveTime(executiveTime) );
		if (null == userData) {
			map.put("teacherInfo",BaseResultCodeEnum.ERR_CODE_30002.getMsg());
		}
		else{
			map.put("teacherInfo", userData);
		}
		
		map.put("performanceInfo", performanceInfo);
		return map;
	}
	
}
